package memory
{
	import flash.utils.getQualifiedClassName;
	
	
	public class MemoryRecovery
	{
		private static var instance:MemoryRecovery
		public function MemoryRecovery()
		{
			if(instance==null)
			{
				instance=this;
			}else
			{
				throw new Error("不能实例化");
			}
		}
		public static function getInstance():MemoryRecovery
		{
			if(instance==null) return new MemoryRecovery();
			return instance;
		}
		//回收事件
		public function gcFun(obj:*,eventname:String,fun:Function):void
		{
			if(obj!=null)
			{
				if(obj.hasEventListener(eventname))
				{
					obj.removeEventListener(eventname,fun);
				}
			}
		}
        public function gcObj(obj:*) : void
        {
        	if(obj!=null)
        	{
	        	switch(getQualifiedClassName(obj))
	        	{
	        		case "org.papervision3d.objects.primitives::Plane":
	        		case "org.papervision3d.objects.primitives::Sphere":
	        		{
						if(obj.parent!=null) obj.parent.removeChild(obj);
						if(obj.material!=null)
						{
							if(obj.material.bitmap!=null)
							{
								obj.material.bitmap.dispose();
								obj.material.bitmap=null;
							}
							obj.material.destroy();
							obj.material=null;
						}
						obj=null;
						break;
	        		}
	        		case "flash.display::Loader":
	        		{
	        			if(obj.parent!=null) obj.parent.removeChild(obj);
						try
						{
							obj.close();
						}catch(e:Error){}
						try
						{
							obj.content["dispose"]();
						}catch(e:Error){}
						obj.unload();
	        			break;
	        		}
	        		case "ca.turbulent.media::Pyro":
	        		{
						obj.kill();
						if(obj.parent!=null) obj.parent.removeChild(obj);
						obj=null;
						break;
	        		}
	        		default:
	        		{
	        			if(obj.hasOwnProperty("parent"))
	        			{
	        				if(obj.parent!=null)obj.parent.removeChild(obj);
	        			}
	        			if(obj.hasOwnProperty("dispose")) obj.dispose();
	        			obj=null;
	        			break;
	        		}
	        	}
        	}
        }// end function

	}
}